package cn.bellychang.factorymethod.demo02;


import org.junit.jupiter.api.Test;

/**
 * 两个匿名内部工厂类都实现工厂类，但方法返回不同的服务
 * @author ChangLiang
 * @date 2019/12/12
 */
public class Main {

    public void mobileSayHello(MobileFactory factory){
        factory.produceMobile().sayHello();
    }

    @Test
    public void test(){
        mobileSayHello(Motorola.factory);
        // implementations are completely interchangeable
        // 透明地将某个实现替换为另一个实现
        mobileSayHello(Nokia.factory);
    }

    interface Mobile{
        void sayHello();
    }

    interface MobileFactory{
        Mobile produceMobile();
    }

    static class Motorola implements Mobile{

        @Override
        public void sayHello() {
            System.out.println("motorola mobile");
        }

        public static MobileFactory factory = () -> new Motorola();
    }

    static class Nokia implements Mobile{

        @Override
        public void sayHello() {
            System.out.println("nokia mobile");
        }

        public static MobileFactory factory = () -> new Nokia();
    }
}
